package com.chushouya.config;

import com.chushouya.common.support.SwaggerHeaders;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2 Doc setting
 * @author leejean team
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
@ConditionalOnExpression("${com.chushouya.swagger.enable:false}")
public class Swagger2 {


    @Bean
    public Docket docketAdminProduct() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("admin-product")
                .apiInfo(apiInfo("后台管理系统-商品模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.product.admin"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketAdminManager() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("admin-manager")
                .apiInfo(apiInfo("后台管理系统-基础模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.manager.admin"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketAdminOrder() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("admin-order")
                .apiInfo(apiInfo("后台管理系统-订单模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.order.admin"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }


    @Bean
    public Docket docketApiProduct() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("api-product")
                .apiInfo(apiInfo("API-商品模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.product.api"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketApiManager() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("api-manager")
                .apiInfo(apiInfo("API-基础模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.manager.api"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketApiOrder() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("api-order")
                .apiInfo(apiInfo("API-订单模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.order.api"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }
    @Bean
    public Docket docketApiOrderClerk() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("api-order-clerk")
                .apiInfo(apiInfo("API-订单工程师模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.order.clerk"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketApiManagerClerk() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("api-manager-clerk")
                .apiInfo(apiInfo("API-管理工程师模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.manager.clerk"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketApiOpen() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("api-open")
                .apiInfo(apiInfo("API-开发接口模块", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.open"))
                .paths(PathSelectors.any()).build().globalOperationParameters(SwaggerHeaders.buildAdminHeaderList());
    }

    @Bean
    public Docket docketCommon() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("common")
                .apiInfo(apiInfo("公共服务", "1.0.0"))
                .select().apis(RequestHandlerSelectors.basePackage("com.chushouya.controller.common"))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(final String name, final String version) {
        return new ApiInfoBuilder().title(name).description("接口文档").version(version).build();
    }

}
